#!/usr/bin/env python
# encoding: utf-8

from django.http import Http404, HttpResponse
from django.template import Context, RequestContext
from django.shortcuts import render_to_response, get_object_or_404
from django.views.generic.list_detail import object_list
from datetime import datetime
from django.conf import settings
from django.views.decorators.cache import cache_page
from ponyfolio.models import SiteMeta, Gig, Screenshot, ContactInfo
from ponyfolio import settings as pony_settings 

#@cache_page(60*60)
def index(request):  
    gigs = Gig.on_site.filter(active=True)
    if pony_settings.GIGS_ORDER:
        gigs = gigs.order_by(pony_settings.GIGS_ORDER)
    gigs = gigs.select_related(depth=1)
    try:
        meta = SiteMeta.on_site.latest()
    except SiteMeta.DoesNotExist:
        meta = None
    try:
        contact = ContactInfo.on_site.latest()
    except ContactInfo.DoesNotExist:
        contact = None
    if pony_settings.STYLE=='blog':
        template = "ponyfolio/index_blog.html"
    else:
        template = "ponyfolio/index.html"
    return object_list(request, 
        queryset = gigs,
        paginate_by = pony_settings.GIGS_PER_PAGE, 
        extra_context = { 'contact':contact, 'meta':meta, },
        template_object_name = 'gig',
        template_name = template
        )
    

def ajax_gig(request, id):
    gig = get_object_or_404(Gig, id=id)
    template = "ponyfolio/gig.html"
    return render_to_response(template, {
            'gig':gig,
    }, context_instance=RequestContext(request))
    